# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.departmentDao import DepartmentDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertDept', methods=['POST'])
def insert_deptNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'deptName': request.json.get('deptName', ''),
        'deptPcode': request.json.get('deptPcode', ''),
        'deptCode': request.json.get('deptCode', ''),
        'deptIsLock': request.json.get('deptIsLock', 0),
        'deptSort': request.json.get('deptSort', 99),
        'deptAddress': request.json.get('deptAddress', ''),
        'leveCode': request.json.get('leveCode', ''),
        'provinceId': request.json.get('provinceId', ''),
        'cityId': request.json.get('cityId', ''),
        'districtId': request.json.get('districtId', ''),
        'deptUrl': request.json.get('deptUrl', ''),
        'desUrl': request.json.get('desUrl', ''),
        'extractFrom': request.json.get('extractFrom', '')
    }

    info = DepartmentDao().insertDept(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findDeptById', methods=['POST'])
def find_dept_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    deptId = request.json.get('deptId', '')
    info = DepartmentDao().findDeptById(deptId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findDeptByCondition', methods=['POST'])
def find_dept_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = DepartmentDao().findDeptByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                      multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/deleteDeptByIds', methods=['POST'])
def delete_dept_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = DepartmentDao().deleteDept(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateDeptById', methods=['POST'])
def update_dept_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        deptId = request.json['deptId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入修改信息的ID"}})
    oldInfo = DepartmentDao().selectDept(deptId)

    info = {
        'deptId': request.json['deptId'],
        'deptName': request.json.get('deptName', oldInfo[1]),
        'deptPcode': request.json.get('deptPcode', oldInfo[2]),
        'deptCode': request.json.get('deptCode', oldInfo[3]),
        'deptIsLock': request.json.get('deptIsLock', oldInfo[4]),
        'deptSort': request.json.get('deptSort', oldInfo[5]),
        'deptAddress': request.json.get('deptAddress', oldInfo[6]),
        'leveCode': request.json.get('leveCode', oldInfo[7]),
        'provinceId': request.json.get('provinceId', oldInfo[8]),
        'cityId': request.json.get('cityId', oldInfo[9]),
        'districtId': request.json.get('districtId', oldInfo[10]),
        'deptUrl': request.json.get('deptUrl', oldInfo[11]),
        'desUrl': request.json.get('desUrl', oldInfo[12]),
        'extractFrom': request.json.get('extractFrom', oldInfo[13]),

    }

    info = DepartmentDao().updateDept(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
